"""
FileName：imageCode_util.py
Description：
Author：gaojiahui
Time：2025/8/20 15:34
Website：https://gitee.com/gao-jiahui
"""
import base64
import json

import requests

from config.settings import settings
from core.api_client import api_client


class ImageCode:

    def __init__(self):
        # 图片地址
        self.image_path = "/auth/code/image"
        # 解析图片验证码的地址
        self.image_code_url = "http://api.ttshitu.com/predict"

    def get_image(self,random_str):
        """获取图片"""
        data = {'randomStr': random_str}
        response = api_client.get(self.image_path,params=data)
        image_byte = response.content  # 获取响应结果的二进制
        base64_data = base64.b64encode(image_byte)
        b64 = base64_data.decode()
        return b64

    def get_image_code(self,random_str):
        """解析图片，获取验证码"""
        b64 = self.get_image(random_str=random_str)
        data = {"username": "gaojiahui", "password": "66666666Hh", "typeid": "1005", "image": b64}
        response = json.loads(requests.post(url=self.image_code_url, json=data).text)
        if response['success']:
            return response["data"]["result"]
        else:
            return response["message"]